/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IpChat_For_Testing;

/*
 *
 * @author lubinchen
 */

import java.net.*;
import java.io.*;

public class ChatClient implements Runnable
{  public Socket socket              = null;
   private Thread thread              = null;
   private DataInputStream  console   = null;
   private DataOutputStream streamOut = null;
   private ChatClientThread client    = null;
   private IpChat_Constructor call=null;
   public String clientname="noFoundName";
   public Integer Team=-1;

   // host is requesting to add a new client of serverIp= serverInetAddress
   public ChatClient(InetAddress serverNameInetAddress, int serverPort, String ClientName, IpChat_Constructor call_in, Integer team)
   {

      int t=1;
      call= call_in;
      clientname= ClientName;
      Team=team;

      while(t<=2){
      try
      {  socket = new Socket(serverNameInetAddress, serverPort);
         call.connection_successful(serverNameInetAddress.getHostAddress());

         t=100;
         IpChat_Constructor.clientCount++;
         start();
         
      }
      catch(UnknownHostException uhe)
      { t++; 
        //System.out.println("Host unknown: " + uhe.getMessage());
      }
      catch(IOException ioe)
      { t++;
        //System.out.println("Unexpected exception: " + ioe.getMessage());
      }
      }


    }
   


   // only standing by good by message to remove
   public void handle(String msg)
   {  if (msg.equals(".bye"))
      {  System.out.println("Good bye. Press RETURN to exit ...");
         stop();
      }
      else
         System.out.println(msg);
   }




   // sending keyboard input out to foreign clients
   public void run()
   { }


   public void newtext(String msg){

       System.out.println("@ChatClient. newtext() visited !");
                String PacketHead="mode:p2p;type:chat;param:2;chat_msg:";
                String Packet_msg=PacketHead.concat(msg);
                Packet_msg=Packet_msg.concat(";");



                System.out.println("\n");
                System.out.println("@ChatClient. New text from GUI input !");
                try{
                            System.out.println("@ChatClient. full msg sent is = "+Packet_msg );
                            streamOut.writeUTF(Packet_msg);
                            IpChat_Main.has_new_text=false;
                            streamOut.flush();
                 }
                 catch(IOException ioe)
                 {  System.out.println("Sending error: " + ioe.getMessage());
                    stop();
                 }

   }


   public void start() throws IOException
   {
      //console   = new DataInputStream(System.in);
      streamOut = new DataOutputStream(socket.getOutputStream());

      if (thread == null)
      {  client = new ChatClientThread(this,  socket, clientname);
         thread = new Thread(this);
         thread.start();
      }
   }



   public void stop()
   {  if (thread != null)
      {  thread.stop();
         thread = null;
      }
      try
      {  if (console   != null)  console.close();
         if (streamOut != null)  streamOut.close();
         if (socket    != null)  socket.close();
      }
      catch(IOException ioe)
      {  System.out.println("Error closing ..."); }
      client.close();
      client.stop();
   }


}

